var levfmJs = {};

(function () {
    'use strict';

    jQuery(function () {
        appJs.init();
        levfmJs.formDataInit();
    });

    var appJs = {
        init: function () {
            Levme.onClick('.showDataMain img', function () {
                if (jQuery(this).parents('a').length > 0) return;
                Levme.photoShow(jQuery(this).attr('src'));
            });

            appJs.srhkey();
        },
        srhkey:function () {
            jQuery(document).on('keyup', '.srhkeyInput', function (e) {
                if (e.which === 13) {
                    var url = window.location.href;
                    !this.value && url.indexOf('srhkey=') <0 ? levtoast('请输入搜索内容') :
                        (window.location = changeUrlArg(url, 'srhkey', this.value));
                }
            })
        }
    };

    levfmJs = {
        formDataInit:function () {
            var listShowFieldsObj = '.settings___listShowFields-box';
            var listShowFields = null;
            setFields(jQuery(listShowFieldsObj).html());

            var inputTxt = {};
            var srhkeyInput = '.field-settingsmodel-srhkey input.form-control';

            jQuery(document).on('change', '.form-control', function () {
                var name = this.name;
                if (name) {
                    setFields(jQuery(listShowFieldsObj).html());
                    name = name.indexOf('[') >0 ? name.split('[')[1].replace(']', '') : name;
                    listShowFields.indexOf(name) >0 && setInputTxt(name, this.value);
                }
            });

            Levme.onClick('#saveForm, .getInputSrhkeyBtn', function () {
                setFields(jQuery(listShowFieldsObj).html());
                setSrhkeyValue(this);
            });

            setInputTxt('srhkey', Levme.jQuery(srhkeyInput).val());
            function setInputTxt(inputname, txt) {
                if (txt !== undefined) inputTxt[inputname] = txt;
            }

            listShowFields && setSrhkeyValue();
            function setSrhkeyValue(btnObj) {
                if (listShowFields.length > 0) {
                    for (var k in listShowFields) {
                        var obj = Levme.jQuery('.field-settingsmodel-'+ listShowFields[k] +' .form-control');
                        var val = obj.find('option:selected').text() || obj.val();
                        if (!val) {
                            val = '';
                            Levme.jQuery('.field-settingsmodel-'+ listShowFields[k] +' label input:checked').each(function () {
                                this.value !== undefined && (val += this.value + ' ');
                            });
                        }
                        val && setInputTxt(listShowFields[k], val);
                    }
                }
                if (jQuery(btnObj).hasClass('getInputSrhkeyBtn') || !Levme.jQuery(srhkeyInput).val() || Levme.jQuery(srhkeyInput).val().indexOf('#') !== 0) {
                    var valstr = '';
                    for (var k2 in inputTxt) {
                        inputTxt[k2] && k2 === 'srhkey' && (inputTxt[k2] = inputTxt[k2].split(' ')[0]);
                        inputTxt[k2] && (valstr += inputTxt[k2] + ' ');
                    }
                    Levme.jQuery(srhkeyInput).val(valstr);
                }
            }
            function setFields(string) {
                string && (listShowFields = JSON.parse(string));
            }
        },
        swiper:function (inputname) {
            var swiper = new Swiper('.slides-input-'+inputname+' .swiper-container', {
                speed: 800,
                spaceBetween: 10,
                //preloadImages: true,
                lazyLoading: true,
                lazyLoadingClass: 'lazy',
                pagination: '.slides-input-'+inputname+' .swiper-pagination',
                paginationClickable: true,
                autoplay: 5000,
                //slidesPerView: 2,
                //onSlideChangeEnd: function () {},
                // onSlideChangeStart: function () {
                //     window.setTimeout(function () { myApp.initImagesLazyLoad('.pages'); }, 201);
                // }
            });
        },
        FmHighc:{},
        opid:0,
        highObjs:null,
        errFormMsg:'',
        clickNum:{},
        voteTimer:null,
        voteTimeoutSubmit:function (formObj, obj, inpt, idx, val) {
            if (levfmJs.voteTimer === 1) return;

            levfmJs.voteTimer && window.clearTimeout(levfmJs.voteTimer);
            levfmJs.voteTimer = window.setTimeout(function () {
                if ( levfmJs.clickNum[inpt] && levfmJs.clickNum[inpt][val] > 30 ) {
                    levfmJs.voteTimer = 1;
                    jQuery(formObj).find('input[name="votenums"]').val(JSON.stringify(levfmJs.clickNum));
                    levfmJs.voteSubmit(formObj, obj, inpt, idx);
                }else {
                    Levme.showNotices('支持失败，未达标30次！左滑再来一次', 'error', 3000);
                }
            }, 5000);
        },
        voteSubmit:function (formObj, obj, inpt, idx, val) {
            Levme.dosaveForm.dosubmit(formObj, function (data, status) {
                if (status >0) {
                    if (val) {
                        if (data.repeat) {
                            levtoast('恭喜，投票成功！猛戳按键大力支持');
                            levfmJs.voteTimer = null;
                            levfmJs.clickNum = {};
                            levfmJs.voteTimeoutSubmit(formObj, obj, inpt, idx, val);
                        }
                    }else {
                        Levme.showNotices('恭喜，本轮成功大力支持！', 'success', 6000);
                    }
                    levfmJs.FmHighc[inpt][2][idx] += 1;
                    levfmJs.FmHighc[inpt][3] = 1;
                    levfmJs.setFmHighcharts(inpt,
                        levfmJs.FmHighc[inpt][0],
                        levfmJs.FmHighc[inpt][1],
                        levfmJs.FmHighc[inpt][2]);
                    levfmJs.opid = data.opid;
                    jQuery(obj).find('tpnum').html(levfmJs.FmHighc[inpt][2][idx]);
                }else {
                    levfmJs.errFormMsg = (data.message || '失败，刷新页面重试')+ status;
                }
            });
        },
        setFmHighcharts: function (inputname, subtitle, chartCates, chartData, title) {
            levfmJs.highObjs ||
            Levme.onClick('.doVoteBtn', function () {
                if (levfmJs.errFormMsg) {
                    levtoast(levfmJs.errFormMsg);
                    return;
                }
                var obj = this;
                var inpt = jQuery(obj).attr('inputname');
                var val = jQuery(obj).attr('val');
                var idx = parseInt(jQuery(obj).attr('idx')) || Levme.jQuery('.tab.active .doVoteBtn').index(obj);
                var formObj = jQuery(obj).parents('#saveForm').eq(0);
                var action = formObj.attr('action') || window.location.href;
                levfmJs.opid &&
                formObj.attr('action', changeUrlArg(action, 'opid', levfmJs.opid));
                Levme.jQuery('.formFieldBox.field-settingsmodel-'+ inpt + ' inpt .form-control').val(val);

                jQuery(obj).hasClass('doSubmitv') && !levfmJs.FmHighc[inpt][3] &&
                levfmJs.voteSubmit(formObj, obj, inpt, idx, val);

                if ( !levfmJs.clickNum[inpt] ) {
                    levfmJs.clickNum[inpt] = {};
                }
                if (!levfmJs.clickNum[inpt][val]) {
                    levfmJs.clickNum[inpt][val] = 1;
                }
                levfmJs.clickNum[inpt][val] += 1;
                if (levfmJs.FmHighc[inpt][3]) {
                    levtoast(levfmJs.clickNum[inpt][val]);
                    levfmJs.FmHighc[inpt][2][idx] += 1;
                    levfmJs.setFmHighcharts(inpt,
                        levfmJs.FmHighc[inpt][0],
                        levfmJs.FmHighc[inpt][1],
                        levfmJs.FmHighc[inpt][2]);
                }
            });

            if ( !levfmJs.FmHighc[inputname] ) {
                levfmJs.FmHighc[inputname] = [subtitle, chartCates, chartData];
            }else {
                subtitle = levfmJs.FmHighc[inputname][0];
                chartCates = levfmJs.FmHighc[inputname][1];
                chartData = levfmJs.FmHighc[inputname][2];
            }

            Levme.script.Highcharts(setHi) && setHi();

            function setHi() {
                levfmJs.highObjs = Highcharts.chart('vote-container-' + inputname, {
                    chart: {
                        type: 'column'
                    },
                    title: {
                        text: title || ""
                    },
                    subtitle: {
                        text: subtitle
                    },
                    legend: {
                        enabled: false,
                    },
                    xAxis: {
                        //categories: ['苹果', '橘子', '梨', '葡萄', '香蕉'],
                        categories: chartCates || false,
                    },
                    yAxis: {
                        title: ''
                    },
                    credits: {
                        enabled: false
                    },
                    plotOptions: {
                        column: {
                            // 关于柱状图数据标签的详细配置参考：https://api.hcharts.cn/highcharts#plotOptions.column.dataLabels
                            dataLabels: {
                                enabled: true,
                                // verticalAlign: 'top', // 竖直对齐方式，默认是 center
                                inside: true
                            }
                        }
                    },
                    series: [{
                        name: '得票数',
                        //data: [5, 3, 4, 7, 2],
                        data: chartData || false,
                    }]
                });
            }
        },
    };
})();